package TUILag;
import CtrLag.*;
import ModelLag.*;

/**
  * Handles the DVD TUI in the system
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class DVDTUI
{
    private DVDCtr dCtr;
    private InputReader reader;

    /**
     * Constructor for objects of class DVDUI
     */
    public DVDTUI(){
        dCtr = new DVDCtr();
        reader = InputReader.getInstance();
    }
    
    /**
     * Runs the personTUI 
     */
    public void run(){
        printMenu();
        boolean finished = false;
        while(!finished) {
            String choise = reader.getInput();
            switch(choise){
                case "1":
                    addNewDVD();
                    break;
                case "2":
                    updateDVD();
                    break;
                case "3":
                    deleteDVD();
                    break;
                case "4":
                    findDVD();
                    break;
                case "5":
                    addCopy();
                    break;
                case "6":
                    findCopy();
                    break;
                case "7":
                    deleteCopy();
                    break;
                case "8":
                    finished = true;
                    break;
                default:
                    System.out.println("Not a command");
                    break;
            }
            printMenu();
        }
    }
    
     /**
     * Prints the DVD menu to the terminal
     */
    private void printMenu(){
        System.out.println("** DVD **");
        System.out.println("1. Add new DVD");
        System.out.println("2. Update DVD");
        System.out.println("3. Delete DVD");
        System.out.println("4. Find DVD");
        System.out.println("5. Add copy of DVD");
        System.out.println("6. Find copy of DVD");
        System.out.println("7. delete copy of DVD");
        System.out.println("8. Main menu");
    }
    
    /**
     * Adds a new DVD to the system
     */
    private void addNewDVD(){
        // get the info
        String barcode = reader.getInputWithLabel("Barcode");
        String title = reader.getInputWithLabel("Title");
        String artist = reader.getInputWithLabel("Artist");
        String publicationDate = reader.getInputWithLabel("Publication date");
        
        // Send the message to the controller:
        dCtr.createDVD(barcode, title, artist, publicationDate);
        // print message to the terminal 
        System.out.println("DVD was created!");
    }
    
    /**
     * Update a DVD based on barcode
     */
    private void updateDVD(){
        String barcode = reader.getInputWithLabel("Barcode");

        try{
            // find the person in the system
            DVD d = dCtr.findDVD(barcode);
            // edit message 
            System.out.println("Edit: " + d.getTitle() + "By: " + d.getArtist());
            // get new values
            String newTitle = reader.getInputWithLabel("New title");
            String newArtist = reader.getInputWithLabel("New artist");
            String newPublicationDate = reader.getInputWithLabel("New publicationdate");
            
            // send the message to the controller
            dCtr.updateDVD(barcode, newTitle, newArtist, newPublicationDate);
            // print message to the terminal 
            System.out.println("DVD was updated!");
        }catch(Exception e){
            // if no person where found
            System.out.println("DVD not found!");
        }
    }
    
    /**
     * Prints a DVD´s information to the terminal
     * 
     */
    private void findDVD(){
        // the barcode of the DVD
        String barcode = reader.getInputWithLabel("Barcode");
        try{
            DVD d = dCtr.findDVD(barcode);
            System.out.println("Title: " + d.getTitle());
            System.out.println("Artist: " + d.getArtist());
            System.out.println("Barcode: " + d.getBarcode());
            System.out.println("Publicationdate: " + d.getPublicationDate());
        }catch(Exception e){
            System.out.println("DVD not found!");
        }
    }
    
    /**
     * Delete a DVD from the system
     */
    private void deleteDVD(){
        String barcode = reader.getInputWithLabel("Barcode");

        try{
            dCtr.deleteDVD(barcode);
            System.out.println("DVD was deleted!");
        }catch(Exception e){
            System.out.println("DVD not found!");
        }
    }
    
    /**
     * Add a new copy of a DVD
     */
    private void addCopy(){
        // get the info
        String serialNumber = reader.getInputWithLabel("Serialnumber");
        String purchaseDate = reader.getInputWithLabel("Purchase date");
        String purchasePrice = reader.getInputWithLabel("Purchase price");
        String barcode = reader.getInputWithLabel("DVD barcode");
        
        // Send the message to the controller:
        dCtr.createCopy(barcode, serialNumber, purchaseDate, Double.parseDouble(purchasePrice));
        // print message to the terminal 
        System.out.println("A copy of " + dCtr.findDVD(barcode).getTitle() + "was created!");
    }
    
    private void findCopy(){
        // the serialnumber of the copy the copy is related to
        String serialNumber = reader.getInputWithLabel("Serialnumber");
        try{
            Copy c = dCtr.findCopy(serialNumber);
            DVD d = dCtr.findDVDfromCopy(serialNumber);
            System.out.println("DVD: " + d.getTitle());
            System.out.println("Serial number: " + c.getSerialNumber());
            System.out.println("Purchase date: " + c.getPurchaseDate());
            System.out.println("Purchase price: " + c.getPurchasePrice());
            System.out.println("Is lent out? " + c.getState());
        }catch(Exception e){
            System.out.println("Copy not found!");
        }  
    }
    
     /**
     * Delete a Copy of a DVD from the system
     */
    private void deleteCopy(){
        String serialNumber = reader.getInputWithLabel("Copy serialnumber");

        try{
            // send message to the controller
            dCtr.deleteCopy(serialNumber);
            System.out.println("Copy was deleted!");
        }catch(Exception e){
            System.out.println("Copy not found!");
        }
    }
    
}
